<?php

namespace app\index\service;

use app\index\model\LunwenTransactionRecordModel;
use app\index\model\ZhiniaoUserModel;
use app\lunwen\model\LunwenWordDocumentModel;
use think\facade\Db;

class LunwenTransactionRecordServer
{



    // 充值操作
    public static function recharge(ZhiniaoUserModel $user, float $amount, float $gift_amount, $remark = ''): ?LunwenTransactionRecordModel
    {
        // 开启事务
        Db::startTrans();
        try {
            // 更新用户余额
            $user->balance += $amount + $gift_amount;
            $user->save();
            
            // 添加充值记录
            $rechargeRecord = new LunwenTransactionRecordModel();
            $rechargeRecord->uid = $user->id;
            $rechargeRecord->amount = $amount;
            $rechargeRecord->transaction_type = LunwenTransactionRecordModel::TRANSACTION_TYPE_RECHARGE;
            $rechargeRecord->gift_amount = $gift_amount;
            $rechargeRecord->remark = $remark;
            $rechargeRecord->save();

            // 提交事务
            Db::commit();

            return $rechargeRecord;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return null;
        }
    }

    // 抵扣操作
    public function deduct(ZhiniaoUserModel $user, float $amount, $remark = ''): ?LunwenTransactionRecordModel
    {
        // 开启事务
        Db::startTrans();
        try {
            // 检查用户余额是否充足
            if ($user->balance < $amount) {
                throw new \Exception("用户余额不足");
            }
            // 更新用户余额
            $user->balance -= $amount;
            $user->save();
            // 添加抵扣记录
            $transactionRecord = new LunwenTransactionRecordModel();
            $transactionRecord->uid = $user->id;
            $transactionRecord->transaction_type = LunwenTransactionRecordModel::TRANSACTION_TYPE_DEDUCT;
            $transactionRecord->amount = $amount;
            $transactionRecord->remark = $remark;
            // $transactionRecord->recharge_record_id = LunwenTransactionRecordModel::getLastRechargeRecord($user->id)->id;
            $transactionRecord->save();
            // 提交事务
            Db::commit();
            return $transactionRecord;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return null;
        }
    }


    // 创建写作任务，并进行抵扣
    public static function addAiWrite($title, $word_num, ZhiniaoUserModel $user){
        // 开启事务
        Db::startTrans();
        try {
            // 检查用户余额是否充足
            if ($user->aiwrite_num < 1) {
                throw new \Exception("当前写作次数不足，请先购买写作次数");
            }
            // 更新用户余额
            $user->aiwrite_num -= 1;
            $user->save();

            // 创建论文任务
            $lunwenWordDocumentModel = new LunwenWordDocumentModel();
            $lunwenWordDocumentModel->file_name = $title;
            $lunwenWordDocumentModel->title = $title;
            $lunwenWordDocumentModel->uid = $user->id;
            $lunwenWordDocumentModel->is_add_summary = 0;
            $lunwenWordDocumentModel->is_add_references = 0;
            $lunwenWordDocumentModel->is_add_keyword = 0;
            $lunwenWordDocumentModel->word_num = $word_num;
            $lunwenWordDocumentModel->is_add_en_keyword = 0;
            $lunwenWordDocumentModel->document_type = LunwenWordDocumentModel::DOCUMENT_TYPE_AIWRITE;
            $lunwenWordDocumentModel->save();
            // 提交事务
            Db::commit();
            return $lunwenWordDocumentModel;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return null;
        }
    }


    // 创建论文任务，并进行抵扣
    public static function lunwenCreateAndDeduct($title, ZhiniaoUserModel $user, float $amount, $word_num = 6000, $extra_data = []): ?LunwenWordDocumentModel
    {
        if(isset($extra_data['file_md5']) && isset($extra_data['title_md5'])){
            $lunwenWordDocumentModel = LunwenWordDocumentModel::where('file_md5', $extra_data['file_md5'])->where('title_md5', $extra_data['title_md5'])->find();
            if($lunwenWordDocumentModel){
                return $lunwenWordDocumentModel; 
            }
        }
        // 开启事务
        Db::startTrans();
        try {
            // 检查用户余额是否充足
            if ($user->balance < $amount) {
                throw new \Exception("用户余额不足");
            }
            // 更新用户余额
            $user->balance -= $amount;
            $user->save();
            // 添加抵扣记录
            $transactionRecord = new LunwenTransactionRecordModel();
            $transactionRecord->transaction_type = $user->id;
            $transactionRecord->uid = $user->id;
            $transactionRecord->transaction_type = LunwenTransactionRecordModel::TRANSACTION_TYPE_DEDUCT;
            $transactionRecord->amount = $amount;
            $transactionRecord->remark = sprintf("编写论文【%s】消耗【%s】", $title, $amount);

            // $transactionRecord->recharge_record_id = LunwenTransactionRecordModel::getLastRechargeRecord($user->id)->id;
            $transactionRecord->save();
            
            // 创建论文任务
            $lunwenWordDocumentModel = new LunwenWordDocumentModel();
            foreach ($extra_data as $key => $value) {
                $lunwenWordDocumentModel->$key = $value;
            }
            $lunwenWordDocumentModel->file_name = $title;
            $lunwenWordDocumentModel->title = $title;
            $lunwenWordDocumentModel->word_num = $word_num;
            $lunwenWordDocumentModel->uid = $user['id'];
            $lunwenWordDocumentModel->save();

            // 提交事务
            Db::commit();

            return $lunwenWordDocumentModel;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return null;
        }
    }
}
